package com.yh.service.impl;

import com.yh.common.DateUtil;
import com.yh.config.properties.ProducerProperties;
import com.yh.service.IOrderService;
import lombok.AllArgsConstructor;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author yh
 */
@Service
@AllArgsConstructor
public class OrderServiceImpl implements IOrderService {
    private final DefaultMQProducer defaultMQProducer;
    private static final Logger LOGGER = LoggerFactory.getLogger(OrderServiceImpl.class);
    private final ProducerProperties producerProperties;

    /**
     * 消息级别对应时间 messageDelayLevel=1s 5s 10s 30s 1m 2m 3m 4m 5m 6m 7m 8m 9m 10m 20m 30m 1h 2h
     * @param proId
     * @throws InterruptedException
     * @throws RemotingException
     * @throws MQClientException
     * @throws MQBrokerException
     */
    @Override
    public void buyPro(Long proId) throws InterruptedException, RemotingException, MQClientException, MQBrokerException {
        Message message = new Message(producerProperties.getSubscribe(), "TestTag", proId.toString().getBytes());
        message.setDelayTimeLevel(5);
        defaultMQProducer.send(message);
        LOGGER.info("时间="+ DateUtil.getTime(new Date())+"  消息发送成功" + message);

    }

    @Override
    public void sendOrder() {

    }
}
